# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 15:25:17 2019

@author: jm
"""
import requests
import pandas as pd

app_id = 'f1a38b71'
app_key = '46651f612db0406771ccfd34c7431dee'

params = {'app_id': app_id,
          'app_key': app_key,
          'pageSize': 1000,
          'pageNum': 0,
          'rated': 1,
        }

baseurl = 'https://api.data.charitynavigator.org/v2'
api = f'/Organizations' # f denotes that there's a variable in url (as in {ein} in other file)
url = f'{baseurl}{api}'

#%%
eins = []
pages = range(10)
for page in pages:
    print(page)
    params['pageNum'] += 1
    R = requests.get(url, params=params)
    R.raise_for_status()
    responses = R.json()
    for response in responses:
        eins.append((response['charityName'], response['ein']))
#    print(i, response['charityName'], response['ein'])

#%%
data = pd.DataFrame(eins, columns=['name','ein'])
data.to_csv('eins.csv', index=False)


